package com.xonro.common.histrans.sfeg.biz.common;

import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.biz.ProcessTransBiz;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.sfeg.biz.ProcessInfoBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecOneProcessCreateContext;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;

/**
 * @Author:zhuw
 * @Description:
 * @Date 2022/4/15
 */
public abstract class SfegRecordTransBiz extends ProcessTransBiz {
    protected abstract void transProcess0(SfecProcessCreateContext createContext);

    protected abstract String getProcessDefId();
    protected abstract RowMap getMainData();
    //第一个节点定义ID
    protected abstract String getActivityDefId();

    protected abstract String getTitle(HistoryTransContext ctx);

//    protected abstract String getBillCode(HistoryTransContext ctx);

    //单据号
    protected abstract String getEnCode(HistoryTransContext ctx);

    protected boolean initData(SfecProcessCreateContext historyTransContext) {
        return true;
    }


    protected HistoryTransContext initTransContext() {
        SfecProcessCreateContext sfecProcessCreateContext = new SfecProcessCreateContext();
        return sfecProcessCreateContext;
    }
    @Override
    protected boolean transProcess(HistoryTransContext ctx) {
        SfecProcessCreateContext historyTransContext = (SfecProcessCreateContext) ctx;
        if (validateTransed(historyTransContext)) {

            return false;
        }
        boolean b = this.initData(historyTransContext);
        if (!b) {
            return false;
        }
        ProcessInfoBiz.getInstance().addRecordProcessInfo(this.getProcessDefId(), this.getActivityDefId(), getTitle(ctx), historyTransContext);
        if (((historyTransContext).isHasProcess())) {
            this.transProcess0(historyTransContext);
            return true;
        } else {
            return false;

        }
    }

    private boolean validateTransed(SfecProcessCreateContext historyTransContext) {
        String pk = getPk(historyTransContext);
        String outerTable = getOuterTable(historyTransContext);
        return DBSql.getMap("SELECT 1 FROM BO_EU_HISTRANS_LOG WHERE " +
                " OUTER_TABLE = ? AND OUTER_PK = ? ", outerTable, pk) != null;
    }

    //OA表
    protected abstract String getOuterTable(SfecProcessCreateContext historyTransContext);

    //OA表主键
    protected abstract String getPk(SfecProcessCreateContext historyTransContext);

}
